﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Common.Log;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Net;

namespace ZyGames.DZPocker.Bll.Logic
{
	public delegate void NotifyActionHandle(int actionId, List<GameUser> userList, Parameters param, Action<int> callback);
	/// <summary>
	/// 客户端通知管理
	/// </summary>
	public class ClientNotifier
	{
		private static readonly object SyncRoot = new object();
		private static event NotifyActionHandle NotifyHandle;

		/// <summary>
		/// 广播所有在线玩家
		/// </summary>
		/// <param name="actionId"></param>
		/// <param name="parameters"></param>
		/// <param name="callback"></param>
		public static void NotifyOnlineUserAction(int actionId, Parameters parameters, Action<int> callback = null)
		{
			var list = new List<GameUser>();
			new GameDataCacheSet<GameUser>().Foreach((pid, userId, user) =>
			{
				if (user.OnlineDate > DateTime.MinValue)
				{
					list.Add(user);
				}
				return true;
			});
			NotifyAction(actionId, list, parameters, callback);
		}
		/// <summary>
		/// 触发通知 --单个玩家
		/// </summary>
		/// <param name="actionId"></param>
		/// <param name="user"></param>
		/// <param name="parameters"></param>
		/// <param name="callback"></param>
		public static void NotifyAction(int actionId, GameUser user, Parameters parameters, Action<int> callback = null)
		{
			var list = new List<GameUser>();
			list.Add(user);
			NotifyAction(actionId, list, parameters, callback);
		}

		/// <summary>
		/// 通知好多玩家
		/// </summary>
		/// <param name="actionId"></param>
		/// <param name="userList"></param>
		/// <param name="parameters"></param>
		/// <param name="callback"></param>
		public static void NotifyAction(int actionId, List<GameUser> userList, Parameters parameters, Action<int> callback)
		{
			if (NotifyHandle == null)
			{
				lock (SyncRoot)
				{
					if (NotifyHandle == null)
					{
						NotifyHandle += new NotifyActionHandle(DoNotifyAction);
					}
				}
			}
			//NotifyHandle(actionId, userList, parameters, callback);
			NotifyHandle.BeginInvoke(actionId, userList, parameters, callback, null, null);
		}

		private static void DoNotifyAction(int actionId, List<GameUser> userList, Parameters parameters, Action<int> callback)
		{
			List<SessionUser> sessionUsers = userList.ConvertAll(user => new SessionUser(user));
			ActionFactory.SendAsyncAction(sessionUsers, actionId, parameters, g =>
			{
				if (callback != null)
				{
					callback(actionId);
				}
			}).Wait();
		}

		/// <summary>
		/// 获得当前房间内所有的玩家
		/// </summary>
		/// <param name="tableData"></param>
		/// <param name="ignoreUserId"></param>
		/// <returns></returns>
		public static List<GameUser> GetUserList(TableData tableData, int ignoreUserId = 0)
		{
			var userList = new List<GameUser>();
			var cacheSet = new GameDataCacheSet<GameUser>();
			foreach (var player in tableData.PlayersData)
			{
				GameUser user = cacheSet.FindKey(player.GetUserId().ToString());
				if (user != null && user.GetUserId() != ignoreUserId)
				{
					userList.Add(user);
				}
			}
			return userList;
		}
	}
}
